package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for teachers, parents, and students. This class is
 * extended for parents and students to show the relationships between them.
 */
public class UserContainer extends Container {

    // The map of timer buckets for all databases
    private static Hashtable dbStores = new Hashtable();
    // The auto-generated id of the user
    private IntegerItem assignedID =
        new IntegerItem(SchoolDBSQL.USRASSIGNEDID, "", true);
    // The ID of the user as assigned by the school
    private StringItem schoolID =
        new StringItem(SchoolDBSQL.USRSCHOOLID, "", false);
    // The first name of the user.
    private StringItem firstName =
        new StringItem(SchoolDBSQL.USRFNAME, "", false);
    // The last name of the user.
    private StringItem lastName =
        new StringItem(SchoolDBSQL.USRLNAME, "", false);
    // The password of the user.
    private PasswordItem password =
        new PasswordItem(SchoolDBSQL.USRPASSWORD, "", true);
    // The password change required flag
    private BooleanItem pswdChgReq =
        new BooleanItem(SchoolDBSQL.USRPSWDCHGREQ, "FALSE", true);
    // The phone number of the user.
    private StringItem phone = new StringItem(SchoolDBSQL.USRPHONE, "", false);
    // The email of the user.
    private StringItem email = new StringItem(SchoolDBSQL.USREMAIL, "", false);
    // The date and time of the user's last login.
    private DateItem lastLogin =
        new DateItem(SchoolDBSQL.USRLASTLOGIN, "", false);
    // The number of failures trying to log in since the last successful login.
    private IntegerItem failures =
        new IntegerItem(SchoolDBSQL.USRFAILURES, "0", true);
    // The type of user (S, T, P, or A)
    private StringItem type = new StringItem(SchoolDBSQL.USRTYPE, "S", true);
    // The title of the user, such as Mr. or Miss.
    private StringItem title = new StringItem(SchoolDBSQL.USRTITLE, "", false);
    // The name of the picture file for the user
    private StringItem pictureName = new StringItem(SchoolDBSQL.USRPICTURENAME, "", false);
    // The school related to the user
    protected String schoolName = "";

    /**
     * Default constructor
     */
    protected UserContainer() {

    }

    /**
     * Returns a new instance of a container with values pre-loaded from the database
     * corresponding to the user ID passed in as a parameter.
     * @param newUserId The auto-generated ID of the user to load.
     */
    protected UserContainer(String newUserId, String dbName) {
        schoolName = dbName;
        getAssignedID().setValue(newUserId);
        SchoolDBSQL.getSchoolDBSQL(dbName).loadUser(
            getValidationItems(), assignedID);
    }

    public static UserContainer getUserContainer(String uID, String dbName) {
        UserContainer returnVal = null;
        DBStore dbs = null;

        if (uID == "-1") {
            returnVal = new UserContainer();
        }
        else {
            if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
                dbs = new DBStore();
                dbStores.put(dbName, dbs);
                dbs.start();
            }
            if ((returnVal = (UserContainer) dbs.getContainer(uID)) == null) {
                returnVal = new UserContainer();
                returnVal.schoolName = dbName;
                returnVal.getAssignedID().setValue(uID);
                SchoolDBSQL.getSchoolDBSQL(dbName).loadUser(returnVal.getValidationItems(), returnVal.getAssignedID());
                dbs.addContainer(returnVal, uID);
            }
        }


        return returnVal;
    }

    /* (non-Javadoc)
     * @see schoolsource.model.Container#getValidationItems()
     */

    public Item[] getValidationItems() {
        ArrayList al = new ArrayList();
        al.add(assignedID);
        al.add(schoolID);
        al.add(firstName);
        al.add(lastName);
        // al.add(password);
        al.add(phone);
        al.add(email);
        al.add(lastLogin);
        al.add(failures);
        al.add(type);
        al.add(title);
        al.add(pswdChgReq);
        al.add(pictureName);

        return (Item[]) al.toArray(new Item[al.size()]);
    }

    /**
     * Getter for the user's email address.
     * @return The user's email address.
     */
    public StringItem getEmail() {
        return email;
    }

    /**
     * Getter for the number of failed login attempts since the user's last
     * successful login.
     * @return The number of failed login attempts since the user's last successful login.
     */
    public IntegerItem getFailures() {
        return failures;
    }

    /**
     * Getter for the user's first name.
     * @return The user's first name.
     */
    public StringItem getFirstName() {
        return firstName;
    }

    /**
     * Getter for the date and time of the user's last login.
     * @return The date and time of the user's last login.
     */
    public DateItem getLastLogin() {
        return lastLogin;
    }

    /**
     * Getter for the user's last name.
     * @return The user's last name.
     */
    public StringItem getLastName() {
        return lastName;
    }

    /**
     * Getter for the user's password.
     * @return The user's password.
     */
    public PasswordItem getPassword() {
        return password;
    }

    /**
     * Getter for whether the user's password needs to be changed.
     * @return The user's password change required state.
     */
    public BooleanItem getPasswordChangeRequired() {
        return pswdChgReq;
    }

    /**
     * Getter for the user's telephone number.
     * @return The user's telephone number.
     */
    public StringItem getPhone() {
        return phone;
    }

    /**
     * Getter for the user's type (S, T, P, or A)
     * @return The user's type.
     */
    public StringItem getType() {
        return type;
    }

    /**
     * Getter for the user's auto-generated ID.
     * @return The user's auto-generated ID.
     */
    public IntegerItem getAssignedID() {
        return assignedID;
    }

    /**
     * Getter for the user's school-assigned ID.
     * @return The ID assigned to the user by the school.
     */
    public StringItem getSchoolID() {
        return schoolID;
    }

    /**
     * Getter for the user's title.
     * @return The user's title.
     */
    public StringItem getTitle() {
        return title;
    }

    public StringItem getPictureName() {
        return pictureName;
    }

    /**
     * Prints the values of the container.
     */
    public void printItems() {
        SchoolLogger.logger.log(
            Level.INFO,
            "************************************************");
        Item iList[] = getValidationItems();
        for (int j = 0; j < iList.length; j++) {
            if (iList[j] != null) {
                SchoolLogger.logger.log(
                    Level.INFO,
                    iList[j].getFieldName() + ": " + iList[j].toString());
            }
        }
    }

}
